package org.sensmon.core.datamodel;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import org.extdev.device.ltr.LTRCrate;
import org.extdev.libraries.ltr.LTR11APILibraryFacade;
import org.extdev.libraries.ltr.LTRAPIFacadeFactory;
import org.extdev.libraries.ltr.LTRException;
import org.sensmon.gui.LTR11Pane;

public class LTR11Applyer implements Applyer {//TODO check for pattern command

	LTR11Device oldDevice;
	LTR11APILibraryFacade facade = null;
	LTRCrate crate;
	LTR11Pane pane;
	
	
	public LTR11Applyer(LTR11Pane pane) {
		this.pane = pane;
		try {
			oldDevice = (LTR11Device) GlobalContext.getInstance().getValue(LTR11Device.class);
			facade = oldDevice.getFacade();
		} catch (Exception e) {
			oldDevice = null;
			e.printStackTrace();
		}
		pane.addCrateItemChangeListener(new CrateChangeListener());
		pane.addModuleChangeItemListener(new ModuleChangeListener());
	}
	
	@Override
	public void apply() {
		
	}

	@Override
	public void cancel() {
		// TODO Auto-generated method stub

	}
	
	private class CrateChangeListener implements ItemListener {

		@Override
		public void itemStateChanged(ItemEvent e) {
			crate = (LTRCrate) e.getItem();
		}
		
	}
	
	private class ModuleChangeListener implements ItemListener {

		@Override
		public void itemStateChanged(ItemEvent event) {
			if (event.getItem() != null) {
				try {
					facade = LTRAPIFacadeFactory.getInstance().getLTR11ModuleFacade(crate.getSerialNumberOfCrate(), (Short) event.getItem());
				} catch (LTRException exception) {
					// TODO Auto-generated catch block
					exception.printStackTrace();
				}
			}
		}
		
	}
	
}
